/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Clases;

/**
 *
 * @author Ariel
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class CreateDataBase {

  public void CreateDataBase() {
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    String dbName = "TPIBaseDato";
    String dbParam = "create=true"; //la base de datos se creará si no existe todavía
    String connectionURL = "jdbc:derby:" + dbName + ";" + dbParam;
    

    try{
      Class.forName(driver);
    } catch(java.lang.ClassNotFoundException e) {
      e.printStackTrace();
    }
      CrearVocabulario(dbName, connectionURL);
      CrearDocumento(dbName, connectionURL);
      CrearVocabularioXDocumento(dbName, connectionURL);
      System.out.println("La base de datos '" + dbName + "' se ha creado correctamente");
  }
  public  void CrearVocabulario(String dbName,String connectionURL){
      Connection conn = null;
      try {
      conn = DriverManager.getConnection(connectionURL);

      Statement st = conn.createStatement();
      String sqlCreateTableUsers =
             "CREATE TABLE Vocabulario ( " +
              "idVoc INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"+
             "PALABRA VARCHAR(70) not null unique," +
             "FRECUENCIA BIGINT not null," +
             "CONSTRAINT idVoc_key PRIMARY KEY (idVoc)" +
             ")";
      // la sentencia SQL crea una tabla con 3 campos
      st.execute(sqlCreateTableUsers);

      //System.out.println("La base de datos '" + dbName + "' se ha creado correctamente");
    }  catch (Throwable e)  {
      System.out.println("Error al crear la tabla Vocabulario");
      e.printStackTrace();
    } finally {
      try { conn.close(); }
      catch (Throwable t){}
    }
  }
  public  void CrearDocumento(String dbName,String connectionURL){
      Connection conn = null;
      try {
      conn = DriverManager.getConnection(connectionURL);

      Statement st = conn.createStatement();
      String sqlCreateTableUsers =
             "CREATE TABLE Documento ( " +
              "idDoc INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"+
             "NOMBRE VARCHAR(100) not null," +
             "DIR VARCHAR(250) not null," +
             "CONSTRAINT idDoc_key PRIMARY KEY (idDoc)" +
             ")";
      // la sentencia SQL crea una tabla con 3 campos
      st.execute(sqlCreateTableUsers);

      //System.out.println("La base de datos '" + dbName + "' se ha creado correctamente");
    }  catch (Throwable e)  {
      System.out.println("Error al crear la tabla Documento.");
      e.printStackTrace();
    } finally {
      try { conn.close(); }
      catch (Throwable t){}
    }
  }
  public  void CrearVocabularioXDocumento(String dbName,String connectionURL){
      Connection conn = null;
      try {
      conn = DriverManager.getConnection(connectionURL);

      Statement st = conn.createStatement();
      String sqlCreateTableUsers =
             "CREATE TABLE VocabularioXDocumento ( " +
             "voc_id BIGINT not null," +
             "doc_id BIGINT not null" +
             ")";
      // la sentencia SQL crea una tabla con 3 campos
      st.execute(sqlCreateTableUsers);

      
    }  catch (Throwable e)  {
      System.out.println("Error al crear la base de datos '" + dbName + "'");
      e.printStackTrace();
    } finally {
      try { conn.close(); }
      catch (Throwable t){}
    }
  }
}

